#include "gudb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    printf("=== Transaction GuDB Test ===\n");
    
    // Create a temporary directory for testing
    system("mkdir -p /tmp/gudb_txn_test");
    
    // Test 1: Database creation
    printf("1. Testing database creation...\n");
    gudb* db = gudb_open("/tmp/gudb_txn_test", GU_OPEN_CREATE, "admin", "admin");
    if (!db) {
        printf("❌ Failed to create database\n");
        return 1;
    }
    printf("✅ Database created successfully\n");
    
    // Test 2: Transaction creation
    printf("2. Testing transaction creation...\n");
    gudb_txn* txn = gudb_begin(db, GU_TXN_RC);
    if (!txn) {
        printf("❌ Failed to create transaction\n");
        gudb_close(db);
        return 1;
    }
    printf("✅ Transaction created successfully (ID: %lu)\n", txn->xid);
    
    // Test 3: Transaction commit
    printf("3. Testing transaction commit...\n");
    int commit_ret = gudb_commit(txn);
    if (commit_ret != GU_OK) {
        printf("❌ Failed to commit transaction: %d\n", commit_ret);
    } else {
        printf("✅ Transaction committed successfully\n");
    }
    
    // Test 4: Database cleanup
    printf("4. Testing database cleanup...\n");
    int close_ret = gudb_close(db);
    if (close_ret != GU_OK) {
        printf("❌ Failed to close database: %d\n", close_ret);
    } else {
        printf("✅ Database closed successfully\n");
    }
    
    printf("\n=== Transaction Test Complete ===\n");
    
    // Cleanup test directory
    system("rm -rf /tmp/gudb_txn_test");
    
    return 0;
}